/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package net.hnilica.cisteniulic.dao;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import net.hnilica.cisteniulic.dto.Info;

/**
 *
 */
public class InfoDAO {

    protected Logger log = Logger.getLogger(getClass().getName());
    protected EntityManagerFactory emf = Persistence.createEntityManagerFactory(DAO.PU);

    /**
     */
    public List<Info> getAllInfoOneEmail(String email) {
        EntityManager em = emf.createEntityManager();
        try {
            List<Info> listInfo = new ArrayList<Info>();
            Query query = em.createNativeQuery("SELECT email, naz_ulice FROM  " + DAO.t(Info.class)
                    + " WHERE email = :email ", Info.class);
            query.setParameter("email", email);
            listInfo = query.getResultList();
            return listInfo;
        } catch (Exception e) {
            log.log(Level.SEVERE, "Chyba při označování vztahů info pro " + email + ".", e);
            return new ArrayList<Info>();
        } finally {
            em.close();
        }
    }

    public Info getInfo(String email, String nazUlice) {
        return getInfo(new Info(email, nazUlice));
    }

    public Info getInfo(Info info) {
        EntityManager em = emf.createEntityManager();
        try {

            Info infoFromDB = em.find(Info.class, info);
            return infoFromDB;
        } catch (Exception e) {
            log.log(Level.SEVERE, "Chyba při označování infa.", e);
            return new Info();
        } finally {
            em.close();
        }
    }

    public boolean saveOrUpdateInfo(Info info) {

        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            Info infoFromDB = em.find(Info.class, info);
            if (infoFromDB == null || !infoFromDB.equals(info)) {
                em.persist(info);
            } else {
                em.merge(info);
            }
            em.getTransaction().commit();
            return true;
        } catch (Exception e) {
            em.getTransaction().rollback();
            log.log(Level.SEVERE, "Chyba při ukládání infa.", e);
            return false;
        } finally {

            em.close();
        }
    }

    /**
     *
     * @param info
     * @return
     */
    public boolean deleteInfo(Info info) {
        EntityManager em = emf.createEntityManager();
        try {
            em.getTransaction().begin();
            Info infoFromDB = em.find(Info.class, info);
            if (info.equals(infoFromDB)) {

                em.remove(infoFromDB);
                em.getTransaction().commit();
                return true;

            } else {
                em.getTransaction().commit();
                return false;
            }

        } catch (Exception e) {
            em.getTransaction().rollback();
            log.log(Level.SEVERE, "Chyba při mazání infa.", e);
            return false;
        } finally {

            em.close();
        }
    }

}
